//
//  LoadURLImage.swift
//  ImageBasic
//
//  Created by 李煜 on 2024/7/30.
//

import Kingfisher
import SwiftUI

struct LoadURLImage: View {
    var body: some View {
        VStack {
            AsyncImage(url: URL(string: "https://wx1.sinaimg.cn/large/002LTpVoly4gp10w31w5zj60mv0gqgo102.jpg")) { image in
                image.resizable()
            } placeholder: {
                Color.green
            }
            .frame(width: 300, height: 200)
            .clipShape(RoundedRectangle(cornerRadius: 25.0))

            // 2. 动态加载图片 转圈显示进度
            AsyncImage(url: URL(string: "https://wx1.sinaimg.cn/large/002LTpVoly4gp10w31w5zj60mv0gqgo102.jpg")) { phase in
                switch phase {
                case .failure:
                    Image(systemName: "photo")
                        .font(.largeTitle)
                case let .success(image):
                    image.resizable()
                default:
                    ProgressView()
                }
            }
            .frame(width: 200, height: 200)
            .clipShape(Circle())

            // 3. 使用KFImage
            KFImage(URL(string: "https://wx1.sinaimg.cn/large/002LTpVoly4gp10w31w5zj60mv0gqgo102.jpg"))
                .resizable()
                .frame(width: 300,height: 200)
                .cornerRadius(20)
        }
    }
}

#Preview {
    LoadURLImage()
}
